<route lang="yaml">
  meta:
    title: 运维用户
</route>

<script setup>
import axios from 'axios'
import { computed, ref } from 'vue'

// 表格数据
const tableData = ref([])
// 动态提取筛选条件
const uniqueRegions = computed(() => [...new Set(tableData.value.map(item => item.region))])
// 筛选条件
const selectedRegion = ref('')
// 分页相关
const currentPage = ref(1)
const pageSize = 10
// 筛选后的数据
const filteredData = ref(tableData.value)
// 执行搜索，筛选符合条件的数据
function filterData() {
  filteredData.value = tableData.value.filter((item) => {
    return (
      (!selectedRegion.value || item.region === selectedRegion.value)
    )
  })
  // 重置到第一页
  currentPage.value = 1
}
// 计算当前页数据
const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  return filteredData.value.slice(start, start + pageSize)
})
function handlePageChange(page) {
  currentPage.value = page
  fetchData()
}
// 新增对话框
const isAddDialogVisible = ref(false)

function addData() {
  // currentOrderId.value = id; // 设置当前工单号
  isAddDialogVisible.value = true // 打开对话框
}

function confirmAdd() {
  isAddDialogVisible.value = false
  // console.log(`确认发布工单ID: ${currentOrderId.value}, 响应时间: ${responseTime.value}小时`);
  // 添加派遣的实际逻辑
}
// 添加数据
const name = ref('')
const phone = ref('')
const password = ref('')
const region = ref('')
function resetSelection() {
}
async function fetchData(adminUserRegion = null, pageNum = '1', pageSize = '5') {
  try {
    const response = await axios.post('https://www.cqwuyang.cn/ElectricManageService/adminUser/findAllByCondition', {
      adminUserRegion,
      pageNum: currentPage.value,
      pageSize,
    })

    // 如果获取到数据，更新 dialogData
    if (response.data) {
      // Object.assign(dialogData, response.data)
      tableData.value = response.data.data.rows
      console.log('tableData')
      console.log(tableData.value)
    }
  }
  catch (error) {
    console.error('获取数据时出错:', error)
  }
}
async function suspend(adminUserPhone) {
  try {
    const response = await axios.post('https://www.cqwuyang.cn/ElectricManageService/adminUser/suspend', {
      adminUserPhone,
    })
    // 如果获取到数据，更新 dialogData
    if (response.data) {
      console.log('暂停使用结果', response.data)
    }
  }
  catch (error) {
    console.error('获取数据时出错:', error)
  }
}
async function recovery(adminUserPhone) {
  try {
    const response = await axios.post('https://www.cqwuyang.cn/ElectricManageService/adminUser/recovery', {
      adminUserPhone,
    })
    // 如果获取到数据，更新 dialogData
    if (response.data) {
      console.log('恢复使用结果', response.data)
    }
  }
  catch (error) {
    console.error('获取数据时出错:', error)
  }
}
async function deleteUser(adminUserPhone) {
  try {
    const response = await axios.post('https://www.cqwuyang.cn/ElectricManageService/adminUser/delete', {
      adminUserPhone,
    })
    // 如果获取到数据，更新 dialogData
    if (response.data) {
      console.log('删除账户结果', response.data)
    }
  }
  catch (error) {
    console.error('获取数据时出错:', error)
  }
}
onMounted(() => {
  fetchData()
  // 在此处获取订单详情的逻辑
})
</script>

<template>
  <div class="dashboard">
    <div class="header">
      运维用户
    </div>
    <!-- 筛选区域 -->
    <!-- <el-row :gutter="20" class="filter-row">
      <el-col :span="4">
        <el-select v-model="selectedRegion" placeholder="请选择负责区域" clearable>
          <el-option v-for="region in uniqueRegions" :key="region" :label="region" :value="region" />
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" class="search-button" @click="filterData">
          搜索
        </el-button>
        <el-button type="primary" class="search-button" @click="addData">
          新增
        </el-button>
      </el-col>
    </el-row> -->
    <!-- 数据表格 -->
    <el-table :data="tableData" border class="table" style="width: 100%;">
      <el-table-column prop="adminUserCode" label="编号" align="center" />
      <el-table-column prop="adminUserName" label="姓名" align="center" />
      <el-table-column prop="adminUserPhone" label="手机号" align="center" />
      <el-table-column prop="adminUserPwd" label="密码" align="center" />
      <el-table-column prop="adminUserCity" label="负责区域" align="center" />
      <el-table-column prop="actions" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" size="small" class="action-button" @click="suspend(row.adminUserPhone)">
            暂停权限
          </el-button>
          <el-button type="text" size="small" class="action-button" @click="recovery(row.adminUserPhone)">
            恢复权限
          </el-button>
          <el-button type="text" size="small" class="action-button" @click="deleteUser(row.adminUserPhone)">
            删除账户
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增对话框 -->
    <el-dialog v-model="isAddDialogVisible" title="添加" @open="resetSelection">
      <!-- 对话框内容 -->
      <p><strong>姓名：</strong> <el-input v-model="name" style="width: 240px;" placeholder="请输入" clearable /></p>
      <p>
        <strong>手机号：</strong> <el-input v-model="phone" style="width: 240px;" placeholder="请输入" clearable />
      </p>
      <p>
        <strong>密码：</strong> <el-input v-model="password" style="width: 240px;" placeholder="请输入" clearable />
      </p>
      <p><strong>负责区域</strong></p>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-select v-model="selectedProvince" placeholder="请选择省份" style="width: 100%;" @change="onProvinceChange">
            <el-option
              v-for="province in provinces" :key="province.code" :label="province.name"
              :value="province.code"
            />
          </el-select>
        </el-col>
        <el-col :span="8">
          <el-select
            v-model="selectedCity" placeholder="请选择城市" style="width: 100%;" :disabled="!selectedProvince"
            @change="onCityChange"
          >
            <el-option v-for="city in cities" :key="city.code" :label="city.name" :value="city.code" />
          </el-select>
        </el-col>
        <el-col :span="8">
          <el-select v-model="selectedDistrict" placeholder="请选择区县" style="width: 100%;" :disabled="!selectedCity">
            <el-option
              v-for="district in districts" :key="district.code" :label="district.name"
              :value="district.code"
            />
          </el-select>
        </el-col>
      </el-row>
      <template #footer>
        <el-button @click="isAddDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmAdd">
          添加
        </el-button>
      </template>
    </el-dialog>

    <!-- 分页 -->
    <div class="pagination-wrapper">
      <div class="pagination">
        <el-pagination
          size="normal" background layout="prev, pager, next" :total="5000" class="mt-4"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
.dashboard {
  padding: 20px;
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  justify-content: center;
  width: 30%;
  padding: 10px 20px;
  margin-bottom: 20px;

  /* 将分页按钮居中 */
  font-size: 24px;
  font-weight: bold;
  color: #333;
  text-align: center;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.filter-row {
  margin-bottom: 20px;
}

.search-button {
  color: white;
  background: linear-gradient(45deg, #3b82f6, #06b6d4);
  border: none;
  border-radius: 20px;
  box-shadow: 0 4px 8px rgb(0 0 0 / 10%);
}

.search-button:hover {
  background: linear-gradient(45deg, #06b6d4, #3b82f6);
}

.table {
  margin-top: 20px;
  overflow: hidden;
  border-radius: 8px;
}

.table >>> .el-table__header-wrapper th {
  padding: 10px;
  font-size: 14px;
  font-weight: bold;
  color: #333;
  text-align: center;
  background-color: #f3f4f6;
  border-bottom: 2px solid #e5e7eb;
}

.action-button {
  padding: 4px 8px;
  font-weight: bold;
  color: #3b82f6;
  background-color: antiquewhite;
  border-radius: 5px;
  transition: all 0.2s;
}

.action-button:hover {
  background-color: #5382df;
  box-shadow: 0 2px 4px rgb(0 0 0 / 10%);
}

.pagination {
  bottom: 20px;
  display: flex;

  /* 将分页按钮居中 */
  align-items: center;
  justify-content: center;
  padding: 10px 0;
  margin-top: 20px;
  background-color: #f9fafb;
  border-top: 1px solid #e5e7eb;
  border-radius: 0 0 8px 8px;
  box-shadow: 0 -2px 10px rgb(0 0 0 / 5%);
}

.pagination-wrapper {
  position: fixed;
  bottom: 20px;
  left: 280px;
  z-index: 10;
  align-items: center;
  width: 80%;
  padding: 10px 0;
}
</style>
